import 'package:flutter_admin/pages/common/cry/model/order_item_model.dart';
import 'package:flutter/material.dart';
import 'package:flutter_admin/pages/common/cry/cry_data_table.dart';
import 'package:flutter_admin/pages/common/cry/model/page_model.dart';
import 'package:flutter_admin/models/odoo/stock_picking.dart';
import 'package:flutter_admin/models/odoo/stock_production.dart';

class StockProductionList extends StatefulWidget {
  StockPicking? stockPicking;
  late bool flag;
  StockProductionList({required this.stockPicking, Key? key, this.flag = true})
      : super(key: key);
  @override
  _StockProductionListState createState() => _StockProductionListState();
}

class _StockProductionListState extends State<StockProductionList> {
  final GlobalKey<CryDataTableState> tableKey = GlobalKey<CryDataTableState>();
  // final GlobalKey<FormState> formKey = GlobalKey<FormState>();
  final GlobalKey<ScaffoldState> scaffoldStateKey = GlobalKey<ScaffoldState>();
  PageModel page = PageModel(orders: [
    OrderItemModel(column: 'create_date', asc: false),
  ]);
  StockProduction stockProduction = StockProduction();
  List<StockProduction> selectedList = [];
  late List<dynamic> tableData = [];
  @override
  void initState() {
    this.tableData = widget.stockPicking!.detaileds!;
    super.initState();
    WidgetsBinding.instance!.addPostFrameCallback((c) {
      _query();
    });
  }

  @override
  Widget build(BuildContext context) {
    CryDataTable table = CryDataTable(
      key: tableKey,
      title: "数据列表",
      onPageChanged: (firstRowIndex) {
        page.current = (firstRowIndex ~/ page.size + 1);
        _loadData();
      },
      onRowsPerPageChanged: (int size) {
        page.size = size;
        page.current = 1;
        _loadData();
      },
      onSelectChanged: (Map selected) {
        this.setState(() {});
      },
      columns: [
        widget.flag
            ? DataColumn(
                label: Text('产品图号'),
              )
            : DataColumn(
                label: Text('物料编码'),
              ),
        widget.flag
            ? DataColumn(
                label: Text('产品名称'),
              )
            : DataColumn(
                label: Text('物料名称'),
              ),
        widget.flag
            ? DataColumn(
                label: Text('材料型号'),
              )
            : DataColumn(
                label: Text('规格型号'),
              ),
        widget.flag
            ? DataColumn(
                label: Text('数量'),
              )
            : DataColumn(
                label: Text('入库数量'),
              ),
        DataColumn(
          label: Text('计量单位'),
        ),
      ],
      getCells: (m) {
        StockProduction stockProduction = widget.flag
            ? StockProduction.fromMap(m)
            : StockProduction.materiaFromMap(m);
        return [
          DataCell(Text(stockProduction.productCode ?? '--')),
          DataCell(Text(stockProduction.productName ?? '--')),
          DataCell(Text(stockProduction.spec ?? '--')),
          DataCell(Text(stockProduction.qtyDone ?? '--')),
          DataCell(Text(stockProduction.productUomId ?? '--')),
        ];
      },
    );
    var result = Scaffold(
      key: scaffoldStateKey,
      appBar: AppBar(
        leading: Builder(
          builder: (BuildContext context) {
            final ScaffoldState? scaffold = Scaffold.maybeOf(context);
            final ModalRoute<Object?>? parentRoute = ModalRoute.of(context);
            final bool canPop = parentRoute?.canPop ?? false;

            if (canPop) {
              return BackButton();
            } else {
              return SizedBox.shrink();
            }
          },
        ),
        title: Text('产品明细'),
      ),
      body: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          SizedBox(height: 10),
          Expanded(child: SingleChildScrollView(child: table)),
        ],
      ),
    );
    return result;
  }

  _query() {
    // formKey.currentState?.save();
    tableKey.currentState?.pageTo(0);
    _loadData();
  }

  _loadData() {
    List<Map<String, dynamic>> list = [];
    this.tableData.forEach((e) {
      Map<String, dynamic> map = e;
      list.add(map);
    });
    page.records = list;
    page.total = this.tableData.length;
    page.pages = page.total ~/ page.size + 1;
    tableKey.currentState!.loadData(page);
  }
}
